package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JPanel;

import model.GraphAPI;
import model.House;
import model.HouseMeas;
import model.Measurement;
import model.ModelClass;
import model.Variables;
import control.ActListener;
import control.MouseListener;
import control.MouseListenerStatsPanel;
import control.MoveListener;

import javax.swing.JComboBox;
import javax.swing.JCheckBox;
import javax.swing.JRadioButton;
import javax.swing.JLabel;

public class CenterStatsPanel extends JPanel{
	MainF mainF;
	ModelClass modelClass;
	Variables variables;
	ActListener actListener;
	JCheckBox chckbxleftGraf;
	JCheckBox chckbxLeftTemp;
	JCheckBox chckbxLeftLight;
	JCheckBox chckbxLeftCo;   
	JCheckBox chckbxLeftHumidity;
	JCheckBox chckbxRightTemp;
	JCheckBox chckbxRightLight;
	JCheckBox chckbxRightCo;
	JCheckBox chckbxRightHumidity;
	
	JCheckBox chckbxRightGraf;
	JComboBox leftCombo;
	JComboBox rightCombo;
	double[][] data;
	double[][] data1;
	int x1,y1,w1,h1;
	int x2,y2,w2,h2;
	
	int mousex,mousey;
	int lockedRightMX,lockedRightMY;
	int lockedLeftMX,lockedLeftMY;
	boolean leftLocked;
	boolean rightLocked;
	
	
	


	public CenterStatsPanel(MainF mainF) {
	
	setLayout(null);
	setPreferredSize(new Dimension(1280,690));
	setBackground(Color.BLACK);
	this.mainF = mainF;
	this.modelClass = mainF.getModel();
	this.variables = modelClass.getVariables();
	this.actListener = mainF.getAc();
	MoveListener moveL = new MoveListener(mainF);
	addMouseMotionListener(moveL);
	MouseListenerStatsPanel mouseLS = new MouseListenerStatsPanel(mainF);
	addMouseListener(mouseLS);
	
	setName("StatsPanel");
	
	leftLocked = false;
	rightLocked = false;
	
	String [] text = {"testd","sdlkfa"};
	leftCombo = new JComboBox();
	leftCombo.setBounds(138, 98, 200, 20);
	add(leftCombo);
	
	chckbxRightGraf = new JCheckBox("Enable");
	chckbxRightGraf.setForeground(Color.LIGHT_GRAY);
	chckbxRightGraf.setBounds(50, 97, 74, 23);
	chckbxRightGraf.setSelected(true);
	add(chckbxRightGraf);
	
	chckbxleftGraf = new JCheckBox("Enable");
	chckbxleftGraf.setForeground(Color.LIGHT_GRAY);
	chckbxleftGraf.setBounds(700, 97, 78, 23);
	chckbxleftGraf.setSelected(true);
	add(chckbxleftGraf);
	
	rightCombo = new JComboBox();
	rightCombo.setBounds(784, 98, 200, 20);
	add(rightCombo);
	
	chckbxLeftTemp = new JCheckBox("Temp");
	chckbxLeftTemp.setForeground(Color.LIGHT_GRAY);
	chckbxLeftTemp.setBounds(50, 156, 97, 23);
	add(chckbxLeftTemp);
	
	chckbxLeftLight = new JCheckBox("Light");
	chckbxLeftLight.setForeground(Color.LIGHT_GRAY);
	chckbxLeftLight.setBounds(149, 156, 97, 23);
	add(chckbxLeftLight);
	
	chckbxLeftCo = new JCheckBox("CO2");
	chckbxLeftCo.setForeground(Color.LIGHT_GRAY);
	chckbxLeftCo.setBounds(248, 156, 97, 23);
	add(chckbxLeftCo);
	
	chckbxLeftHumidity = new JCheckBox("Humidity");
	chckbxLeftHumidity.setForeground(Color.LIGHT_GRAY);
	chckbxLeftHumidity.setBounds(347, 156, 97, 23);
	add(chckbxLeftHumidity);
	
	chckbxRightTemp= new JCheckBox("Temp");
	chckbxRightTemp.setForeground(Color.LIGHT_GRAY);
	chckbxRightTemp.setBounds(700, 156, 97, 23);
	add(chckbxRightTemp);
	
	chckbxRightLight = new JCheckBox("Light");
	chckbxRightLight.setForeground(Color.LIGHT_GRAY);
	chckbxRightLight.setBounds(799, 156, 97, 23);
	add(chckbxRightLight);
	
	chckbxRightCo = new JCheckBox("CO2");
	chckbxRightCo.setForeground(Color.LIGHT_GRAY);
	chckbxRightCo.setBounds(898, 156, 97, 23);
	add(chckbxRightCo);
	
	chckbxRightHumidity = new JCheckBox("Humidity");
	chckbxRightHumidity.setForeground(Color.LIGHT_GRAY);
	chckbxRightHumidity.setBounds(997, 156, 97, 23);
	add(chckbxRightHumidity);

	
	
	
	x1 = 50;
	x2 = 700;
	
	y1= 200;
	y2= 200;
	
	w1 = 500;
	w2 =500;
	
	h1 = 350;
	h2 = 350;
//	
//	Random r = new Random();
//	data =new double[20][2];
//	for(int j = 0;j<2;j++){
//	for(int i= 0;i<20;i++){
//		data[i][j]=i+r.nextInt(100);
//	}
//	}
//	Random ra = new Random();
//	data1 =new double[40][4];
//	for(int j = 0;j<4;j++){
//	for(int i= 0;i<40;i++){
//		data1[i][j]=i+ra.nextInt(100);
//	}
//	}
	
	
}
	public void refreshAll(){
		leftCombo.removeAllItems();
		rightCombo.removeAllItems();
		ArrayList<House> houses = modelClass.getHouses();
		for(House house : houses){
			leftCombo.addItem(house.getName());
			rightCombo.addItem(house.getName());
		}
	}
	public void updateData(){
		ArrayList<HouseMeas> houseMeas = modelClass.getHouseName(""+leftCombo.getSelectedItem()).getHouseMeasurements();
		
		data =new double[houseMeas.size()][4];
		for(int i = 0; i<houseMeas.size();i++){
			if(chckbxLeftTemp.isSelected())
			data[i][0]= houseMeas.get(i).getCurrentTemp();
			if(chckbxLeftLight.isSelected())
			data[i][1]= houseMeas.get(i).getCurrentLight();
			if(chckbxLeftCo.isSelected())
			data[i][2]= houseMeas.get(i).getCurrentCO2();
			if(chckbxLeftHumidity.isSelected())
			data[i][3]= houseMeas.get(i).getCurrentHumidity();
			System.out.println(data[i][0]);
		}
		houseMeas = modelClass.getHouseName(""+rightCombo.getSelectedItem()).getHouseMeasurements();
		
		data1 = new double[houseMeas.size()][4];
		for(int i = 0; i<houseMeas.size();i++){
			if(chckbxRightTemp.isSelected())
			data1[i][0]= houseMeas.get(i).getCurrentTemp();
			if(chckbxRightLight.isSelected())
			data1[i][1]= houseMeas.get(i).getCurrentLight();
			if(chckbxRightCo.isSelected())
			data1[i][2]= houseMeas.get(i).getCurrentCO2();
			if(chckbxRightHumidity.isSelected())
			data1[i][3]= houseMeas.get(i).getCurrentHumidity();
		}
	}
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		if(!rightLocked && !leftLocked){
		updateData();
		}
		
		
		
		Graphics2D g2 = (Graphics2D) g;
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		
		GraphAPI graphLeft = new GraphAPI();
				graphLeft.getNames().add(0, "Temp");
		graphLeft.getNames().add(1, "Light");
		graphLeft.getNames().add(2, "CO2");
		graphLeft.getNames().add(3, "Humd");
		graphLeft.getGraphType().add(0, false);
		graphLeft.getGraphType().add(1, false);
		
		GraphAPI graphRight = new GraphAPI();
		graphRight.getNames().add(0, "Temp");
		graphRight.getNames().add(1, "Light");
		graphRight.getNames().add(2, "CO2");
		graphRight.getNames().add(3, "Humd");
		graphRight.getGraphType().add(0, false);
		graphRight.getGraphType().add(1, false);
		
		if(!leftLocked){
		if(!chckbxleftGraf.isSelected() && chckbxRightGraf.isSelected()){
		w1 = 1200;
		
		
		
		graphLeft.paintDataGraph(g2, data, x1, y1, w1, h1);
		graphLeft.paintLinesMouse(g2, data, x1, y1, w1, h1,mousex,mousey,1);
		}else if(chckbxleftGraf.isSelected() && chckbxRightGraf.isSelected()){
			w1 = 500;
			graphLeft.paintDataGraph(g2, data, x1, y1, w1, h1);
			graphLeft.paintLinesMouse(g2, data, x1, y1, w1, h1,mousex,mousey,1);
		}}else{
			if(!chckbxleftGraf.isSelected() && chckbxRightGraf.isSelected()){
				w1 = 1200;
				graphLeft.paintDataGraph(g2, data, x1, y1, w1, h1);
				graphLeft.paintLinesMouse(g2, data, x1, y1, w1, h1,lockedLeftMX,lockedLeftMY,1);
				}else if(chckbxleftGraf.isSelected() && chckbxRightGraf.isSelected()){
					w1 = 500;
					graphLeft.paintDataGraph(g2, data, x1, y1, w1, h1);
					graphLeft.paintLinesMouse(g2, data, x1, y1, w1, h1,lockedLeftMX,lockedLeftMY,1);
				}
		}
			
		if(!rightLocked){	
		if(!chckbxRightGraf.isSelected() && chckbxleftGraf.isSelected()){
		w2 = 1200;
		graphRight.paintDataGraph(g2, data1, x1, y2, w2, h2);
		graphRight.paintLinesMouse(g2, data1, x1, y2, w2, h2,mousex,mousey,1);
		}else if(chckbxRightGraf.isSelected() && chckbxleftGraf.isSelected()){
			w2 = 500;
			graphRight.paintDataGraph(g2, data1, x2, y2, w2, h2);
			graphRight.paintLinesMouse(g2, data1, x2, y2, w2, h2,mousex,mousey,1);
		}
		}else{
				if(!chckbxRightGraf.isSelected() && chckbxleftGraf.isSelected()){
				w2 = 1200;
				graphRight.paintDataGraph(g2, data1, x1, y2, w2, h2);
				graphRight.paintLinesMouse(g2, data1, x1, y2, w2, h2,lockedRightMX,lockedRightMY,1);
				}else if(chckbxRightGraf.isSelected() && chckbxleftGraf.isSelected()){
					w2 = 500;
					graphRight.paintDataGraph(g2, data1, x2, y2, w2, h2);
					graphRight.paintLinesMouse(g2, data1, x2, y2, w2, h2,lockedRightMX,lockedRightMY,1);
		}
		}
	}

	public int getX1() {
		return x1;
	}

	public void setX1(int x1) {
		this.x1 = x1;
	}

	public int getY1() {
		return y1;
	}

	public void setY1(int y1) {
		this.y1 = y1;
	}

	public int getX2() {
		return x2;
	}

	public void setX2(int x2) {
		this.x2 = x2;
	}

	public int getY2() {
		return y2;
	}

	public void setY2(int y2) {
		this.y2 = y2;
	}

	public int getMousex() {
		return mousex;
	}

	public void setMousex(int mousex) {
		this.mousex = mousex;
	}

	public int getMousey() {
		return mousey;
	}

	public void setMousey(int mousey) {
		this.mousey = mousey;
	}

	public int getLockedRightMX() {
		return lockedRightMX;
	}

	public void setLockedRightMX(int lockedRightMX) {
		this.lockedRightMX = lockedRightMX;
	}

	public int getLockedRightMY() {
		return lockedRightMY;
	}

	public void setLockedRightMY(int lockedRightMY) {
		this.lockedRightMY = lockedRightMY;
	}

	public int getLockedLeftMX() {
		return lockedLeftMX;
	}

	public void setLockedLeftMX(int lockedLeftMX) {
		this.lockedLeftMX = lockedLeftMX;
	}

	public int getLockedLeftMY() {
		return lockedLeftMY;
	}

	public void setLockedLeftMY(int lockedLeftMY) {
		this.lockedLeftMY = lockedLeftMY;
	}

	public boolean isLeftLocked() {
		return leftLocked;
	}

	public void setLeftLocked(boolean leftLocked) {
		this.leftLocked = leftLocked;
	}

	public boolean isRightLocked() {
		return rightLocked;
	}

	public void setRightLocked(boolean rightLocked) {
		this.rightLocked = rightLocked;
	}

	public JCheckBox getChckbxleftGraf() {
		return chckbxleftGraf;
	}

	public void setChckbxleftGraf(JCheckBox chckbxleftGraf) {
		this.chckbxleftGraf = chckbxleftGraf;
	}

	public JCheckBox getChckbxRightGraf() {
		return chckbxRightGraf;
	}

	public void setChckbxRightGraf(JCheckBox chckbxRightGraf) {
		this.chckbxRightGraf = chckbxRightGraf;
	}

	public int getW1() {
		return w1;
	}

	public void setW1(int w1) {
		this.w1 = w1;
	}

	public int getH1() {
		return h1;
	}

	public void setH1(int h1) {
		this.h1 = h1;
	}

	public int getW2() {
		return w2;
	}

	public void setW2(int w2) {
		this.w2 = w2;
	}

	public int getH2() {
		return h2;
	}

	public void setH2(int h2) {
		this.h2 = h2;
	}
}
